<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem
                        :label="t('shop.market.coupon.center.coupon_id')"
                        type="remoteSelect"
                        v-model="baTable.form.items!.coupon_id"
                        prop="coupon_id"
                        :input-attr="{
                            pk: 'coupon.id',
                            field: 'name',
                            'remote-url': '/admin/shop.market.coupon.coupon/index',
                            teleported: false,
                            tooltipParams: {
                                '优惠描述': 'discount_desc',
                            },
                        }"
                        :placeholder="t('Please select field', { field: t('shop.market.coupon.center.coupon_id') })"
                    />
                    <FormItem
                        :label="t('shop.market.coupon.center.expiry_type')"
                        type="radio"
                        v-model="baTable.form.items!.expiry_type"
                        prop="expiry_type"
                        :data="{
                            content: { '1': t('shop.market.coupon.center.expiry_type 1'), '2': t('shop.market.coupon.center.expiry_type 2') },
                            childrenAttr: {
                                border: true,
                            },
                        }"
                        :placeholder="t('Please select field', { field: t('shop.market.coupon.center.expiry_type') })"
                    />
                    <el-form-item :label="t('shop.market.coupon.center.expiry_time')" v-if="baTable.form.items!.expiry_type == 1" prop="expiry_time">
                        <el-date-picker
                            v-model="baTable.form.items!.expiry_time"
                            type="datetimerange"
                            :start-placeholder="t('Please select field', { field: t('shop.market.coupon.center.start_time') })"
                            :end-placeholder="t('Please select field', { field: t('shop.market.coupon.center.end_time') })"
                            :disabled-date="disabledDate"
                            :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"
                            value-format="YYYY-MM-DD HH:mm:ss"
                        />
                    </el-form-item>
                    <InputNumber
                        v-if="baTable.form.items!.expiry_type == 2"
                        :label="t('shop.market.coupon.center.expiry_days')"
                        prop="expiry_days"
                        v-model.number="baTable.form.items!.expiry_days"
                        :placeholder="t('Please input field', { field: t('shop.market.coupon.center.expiry_days') })"
                        :input-attr="{ step: 1, min: 1, precision: 0 }"
                        :tip="`领券当天开始【${baTable.form.items!.expiry_days || 0}】天内有效`"
                    />
                    <InputNumber
                        :label="t('shop.market.coupon.center.total_num')"
                        prop="total_num"
                        :input-attr="{ step: 1, min: 1 }"
                        v-model.number="baTable.form.items!.total_num"
                        :placeholder="t('Please input field', { field: t('shop.market.coupon.center.total_num') })"
                    />
                    <InputNumber
                        :label="t('shop.market.coupon.center.limit_num')"
                        prop="limit_num"
                        :input-attr="{ step: 1, min: 1 }"
                        v-model.number="baTable.form.items!.limit_num"
                        :placeholder="t('Please input field', { field: t('shop.market.coupon.center.limit_num') })"
                    />
                    <el-form-item :label="t('shop.market.coupon.center.active_time')" prop="active_time">
                        <el-date-picker
                            v-model="baTable.form.items!.active_time"
                            type="datetimerange"
                            :start-placeholder="t('Please select field', { field: t('shop.market.coupon.center.start_time') })"
                            :end-placeholder="t('Please select field', { field: t('shop.market.coupon.center.end_time') })"
                            :disabled-date="disabledDate"
                            value-format="YYYY-MM-DD HH:mm:ss"
                        />
                    </el-form-item>
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type {FormInstance, FormItemRule} from 'element-plus'
import {inject, reactive, ref} from 'vue'
import {useI18n} from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import {useConfig} from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import {buildValidatorData} from '/@/utils/validate'
import InputNumber from '/@/components/shop/form/inputNumber.vue'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    coupon_id: [buildValidatorData({ name: 'required', title: t('shop.market.coupon.center.coupon_id') })],
    total_num: [buildValidatorData({ name: 'required', title: t('shop.market.coupon.center.total_num') })],
    remain_num: [buildValidatorData({ name: 'required', title: t('shop.market.coupon.center.remain_num') })],
    limit_num: [buildValidatorData({ name: 'required', title: t('shop.market.coupon.center.limit_num') })],
    expiry_type: [buildValidatorData({ name: 'required', title: t('shop.market.coupon.center.expiry_type') })],
    expiry_days: [buildValidatorData({ name: 'required', title: t('shop.market.coupon.center.expiry_days') })],
    expiry_time: [buildValidatorData({ name: 'required', title: t('shop.market.coupon.center.expiry_time') })],
    active_time: [buildValidatorData({ name: 'required', title: t('shop.market.coupon.center.active_time') })],
})

const disabledDate = (time: Date) => {
    return time.getTime() < new Date().setHours(0, 0, 0, 0)
}
</script>

<style scoped lang="scss"></style>
